﻿using System.Linq;
using iHouse.Model.Repository.Interface;
using iHouse.Model.Data;
using iHouse.Infrastructure;

namespace iHouse.Model.Repository
{
    public class SqlAlbumRepository : BaseRepository<iHouseDbContext, Album>, IAlbumRepository
    {
        protected override string ContextKey
        {
            get { return "DataContextForAlbum"; }
        }

        protected override PaginationList<Album> GetDataByCategory(int categoryID, int pageIndex, int pageSize)
        {
            return (from i in CurrentDataContext.Albums
                    where i.CategoryID == categoryID
                    orderby i.ID
                    select i)
                    .ToPaginationList(pageIndex, pageSize);
        }

        protected override PaginationList<Album> GetData(PagingCriteria criteria)
        {
            return (from i in CurrentDataContext.Albums
                    orderby i.ID
                    select i)
                    .ToPaginationList(criteria);
        }

        public void CreateAlbum(Album album)
        {
            CurrentDataContext.Albums.Add(album);
            CurrentDataContext.SaveChanges();
        }

        public void UpdateAlbum()
        {
            CurrentDataContext.SaveChanges();
        }

        public void DeleteAlbum(int albumID)
        {
            Album album = CurrentDataContext.Albums.Single(e => e.ID == albumID);

            CurrentDataContext.Albums.Remove(album);
            CurrentDataContext.SaveChanges();
        }

        public Album GetAlbumByID(int albumID)
        {
            return CurrentDataContext.Albums.Single(x => x.ID == albumID);
        }


        public JqGridList<Album> GetAlbumsForList(PagingCriteria criteria)
        {
            return CurrentDataContext.Albums
                .OrderBy(x => x.ID)
                .ToJqGridList(criteria);
        }
    }
}
